# _*_ coding:utf-8 _*_
# 开发团队: Mavericks
# 开发人员：Maverick·Ming
# 开发IDE： PyCharm
#   项目名：Python抓图识别
#   文件名：Util.py
# 开发时间：2021/4/22 20:02
import ctypes
import inspect
import sys  # 系统相关类库
import time  # 时间相关类库
"""
工具类函数集合
"""


def getTimeFormatted():
    """
    获取本地时间
    :return: 格式化后的【时间】字符串
    """
    return time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime())


def printWithTime(*objects):
    """
    自定义拓展的打印函数，使得打印前，打印时间戳
    :param objects: 不定长形参【元组类型】
    :return:
    """
    print(getTimeFormatted()+" ", end='')  # 先打印本地时间戳
    print(*objects)  # 再打印用户需要打印的内容


def inputWithTimePrompt(prompt):
    """
    自定义拓展的输入函数，使得输入前，打印时间戳
    :param prompt: 传入输入提示字符串，如“请输入：”
    :return: 返回组合有时间戳+提示的input函数
    """
    return input(getTimeFormatted()+":"+prompt)


def _asyncRaise(threadId, exctype):
    threadId = ctypes.c_long(threadId)
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(threadId, ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(threadId, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")


def stopThread(thread):
    if thread.isAlive():
        _asyncRaise(thread.ident, SystemExit)